package com.yxt.admin.service.impl;

import com.yxt.admin.entity.OauthClientDetailsEntity;
import com.yxt.admin.entity.SysTenantEntity;
import com.yxt.admin.mapper.OauthClientDetailsMapper;
import com.yxt.admin.service.SysTenantService;
import com.yxt.admin.vo.SelectedVO;
import com.yxt.common.utils.ajax.PageUtils;
import com.yxt.common.utils.ajax.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.yxt.admin.mapper.SysTenantMapper;
import org.springframework.transaction.annotation.Transactional;


@Service("sysTenantService")
public class SysTenantServiceImpl extends ServiceImpl<SysTenantMapper, SysTenantEntity> implements SysTenantService {

    @Autowired
    OauthClientDetailsMapper oauthClientDetailsMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    SysTenantMapper sysTenantMapper;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysTenantEntity> page = this.page(
                new Query<SysTenantEntity>().getPage(params),
                new QueryWrapper<SysTenantEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void cascadeSave(SysTenantEntity sysTenant) {
        OauthClientDetailsEntity oauthClientDetailsEntity = new OauthClientDetailsEntity();
        BeanUtils.copyProperties(sysTenant,oauthClientDetailsEntity);
        oauthClientDetailsEntity.setAuthorizedGrantTypes("password,refresh_token");
        oauthClientDetailsEntity.setScope("read,write");
        oauthClientDetailsEntity.setClientSecret(passwordEncoder.encode(oauthClientDetailsEntity.getClientSecret()));
        oauthClientDetailsMapper.insert(oauthClientDetailsEntity);
        this.save(sysTenant);
    }

    @Override
    @Transactional
    public void cascadeDelete(List<Long> idList) {
        for (Long  id: idList) {
            SysTenantEntity byId = this.getById(id);
            oauthClientDetailsMapper.delete(new QueryWrapper<OauthClientDetailsEntity>()
            .eq("client_id",byId.getClientId()));
        }

        this.removeByIds(idList);
    }

    @Override
    public List<SelectedVO> getTenantName() {
        List<OauthClientDetailsEntity> list = oauthClientDetailsMapper.selectList(  new QueryWrapper<OauthClientDetailsEntity>().select("client_id"));

        List<SelectedVO> selectVOS = list.stream().map(item -> {
            SelectedVO selectedVO = new SelectedVO();
            selectedVO.setLabel(item.getClientId());
            selectedVO.setValue(item.getClientId());
            return selectedVO;
        }).collect(Collectors.toList());
        return selectVOS;
    }


}